package com.zb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.ZjNetworkEmpLevel;
import com.zb.entity.ZjNetworkEmpLevelRelation;
import com.zb.extend.NetworkEmpLevelRelationExtend;
import com.zb.mapper.ZjNetworkEmpLevelRelationMapper;
import com.zb.service.ZjNetworkEmpLevelRelationService;
import com.zb.service.ZjNetworkEmpLevelService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 网点人员等级关联表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2024-02-26
 */
@Service
public class ZjNetworkEmpLevelRelationServiceImpl extends ServiceImpl<ZjNetworkEmpLevelRelationMapper, ZjNetworkEmpLevelRelation> implements ZjNetworkEmpLevelRelationService {
    @Resource
    private ZjNetworkEmpLevelService networkEmpLevelService;

    // 保存或更新人员等级
    @Override
    public void save(Long custAccId, Long networkEmpId, Long networkEmpLevelId) {
        if (null ==networkEmpLevelId || networkEmpLevelId == 0) {
            return;
        }
        QueryWrapper<ZjNetworkEmpLevelRelation> wrapper = new QueryWrapper<>();
        wrapper.select("id", "network_emp_level_id")
                .eq("cust_acc_id", custAccId).eq("network_emp_id", networkEmpId);
        ZjNetworkEmpLevelRelation empLevelRelation = this.getOne(wrapper);
        if (null != empLevelRelation) {
            if (Objects.equals(empLevelRelation.getNetworkEmpLevelId(), networkEmpLevelId)) {
                return;
            }
            empLevelRelation.setNetworkEmpLevelId(networkEmpLevelId);
            this.updateById(empLevelRelation);
            return;
        }
        empLevelRelation = new ZjNetworkEmpLevelRelation();
        empLevelRelation.setId(IdWorker.getId());
        empLevelRelation.setCustAccId(custAccId);
        empLevelRelation.setNetworkEmpId(networkEmpId);
        empLevelRelation.setNetworkEmpLevelId(networkEmpLevelId);
        this.save(empLevelRelation);
    }

    // 根据商家ID & 服务人员ID 获取人员等级信息
    @Override
    public List<NetworkEmpLevelRelationExtend> getByNetworkEmpIds(Long custAccId, List<Long> networkEmpIds) {
        if (CollectionUtil.isEmpty(networkEmpIds)) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjNetworkEmpLevelRelation> wrapper = new QueryWrapper<>();
        wrapper.select("id", "network_emp_id", "network_emp_level_id","manual_level")
                .eq("cust_acc_id", custAccId)
                .in("network_emp_id", networkEmpIds);
        List<ZjNetworkEmpLevelRelation> levelRelationList = this.list(wrapper);
        if (CollectionUtil.isEmpty(levelRelationList)) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjNetworkEmpLevel> empLevelQueryWrapper = new QueryWrapper<>();
        empLevelQueryWrapper.select("id", "name")
                .in("id", levelRelationList.stream().map(ZjNetworkEmpLevelRelation::getNetworkEmpLevelId).toList());
        List<ZjNetworkEmpLevel> empLevelList = networkEmpLevelService.list(empLevelQueryWrapper);
        return levelRelationList.stream().map(empLevelRelatioin -> {
            NetworkEmpLevelRelationExtend extend = new NetworkEmpLevelRelationExtend();
            BeanUtil.copyProperties(empLevelRelatioin, extend);
            empLevelList.stream().filter(f -> Objects.equals(f.getId(), empLevelRelatioin.getNetworkEmpLevelId())).map(ZjNetworkEmpLevel::getName)
                    .findFirst().ifPresent(extend::setNetworkLevel);
            return extend;
        }).toList();
    }

    // 根据商家ID&工程师ID获取工程师等级ID
    @Override
    public Long getNetworkEmpLevelIdByNetworkEmpId(Long custAccId, Long networkId) {
        QueryWrapper<ZjNetworkEmpLevelRelation> wrapper = new QueryWrapper<>();
        wrapper.select("network_emp_level_id")
                .eq("cust_acc_id", custAccId).eq("network_emp_id", networkId);
        ZjNetworkEmpLevelRelation levelRelation = this.getOne(wrapper);
        return null == levelRelation? 0L : levelRelation.getNetworkEmpLevelId();
    }
    // 根据商家ID&工程师ID获取工程师等级修改状态
    @Override
    public Boolean getNetworkEmpLevellevelStausByNetworkEmpId(Long custAccId, Long networkId) {
        QueryWrapper<ZjNetworkEmpLevelRelation> wrapper = new QueryWrapper<>();
        wrapper.select("manual_level")
                .eq("cust_acc_id", custAccId).eq("network_emp_id", networkId);
        ZjNetworkEmpLevelRelation levelStaus = this.getOne(wrapper);
        return  levelStaus != null && levelStaus.getManualLevel() == true;
    }
}
